#include <REGX52.H>
#include "LCD1602.h"
#include "MatrixKey.h"

void main()
{
	unsigned char KeyNum=0;	
	unsigned int mima=0;
	unsigned int num=0;
	LCD_Init();
	LCD_ShowString(1,1,"password");
	while(1)
	{
		KeyNum=MatrixKey();
		if(KeyNum!=0)
		{
			if(KeyNum<=10)
			{
				mima*=10;	//使屏幕能显示四位密码，弊端：首位不能为0
				mima+=KeyNum%10;
				if(num<4)	//设定密码为四位，小于四为才会输入
				{
					LCD_ShowNum(2,1,mima,4);
					num++;
				}
			}
			if(KeyNum==11)	//按下11，判断密码是否与设定的密码相同
			{
				if(mima==5706)
				{
					mima=0;
					num=0;
					LCD_ShowNum(2,1,mima,4);
					LCD_ShowString(1,14,"ok ");
					
				}
				else
				{
					mima=0;
					num=0;
					LCD_ShowNum(2,1,mima,4);
					LCD_ShowString(1,14,"err");
				}
			}
			if(KeyNum==12)	//按下12，清除写入的密码
			{
				mima=0;
				num=0;
				LCD_ShowNum(2,1,mima,4);	
			}
		}
	}
}